﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xts
{
    namespace Util
    {
        public class XTimeAdapter
        {
            protected DateTime x_dtTime;
            protected XTime x_tomTime;

            protected void XTime2DateTime()
            {
                DateTime dt1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                long ticks = x_tomTime.UtcTicks * 10 + dt1970.Ticks;
                x_dtTime = new DateTime(ticks, DateTimeKind.Utc);
            }


            protected void DateTime2XTime()
            {
                DateTime dt1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                TimeSpan spanFrom1970 = x_dtTime - dt1970;
                x_tomTime = new XTime(spanFrom1970.Ticks / 10);    
            }

            public XTimeAdapter(DateTime dtTime)
            {
                if (dtTime.Kind == DateTimeKind.Utc)
                    x_dtTime = dtTime;
                else
                    x_dtTime = dtTime.ToUniversalTime();

                DateTime2XTime();
            }

            public XTimeAdapter(XTime tomTime)
            {
                x_tomTime = tomTime;
                XTime2DateTime();
            }

            public DateTime UtcDateTime { get { return x_dtTime; } }

            public DateTime LocalDateTime { get { return x_dtTime.ToLocalTime(); } }

            public TimeSpan TimeSpan { get { return new TimeSpan(x_tomTime.UtcTicks * 10); } }

            public XTime TomTime { get { return x_tomTime; } }

            public static XTimeAdapter Now()
            {
                return new XTimeAdapter(DateTime.UtcNow);
            }
        }
    }
    
}
